#include"SeqList.h"
void SeqListInit(SL *s1)
{
	s1->arr=NULL;
	s1->size=0;
	s1->capacity = 0;
}

void SeqListPrint(SL* s1)
{
	for (int i = 0; i < s1->size; i++)
	{
		printf("%d ", s1->arr[i]);
	}
	printf("\n");
}


void SeqListCheckcapacity(SL*s1)
{
	if (s1->size == s1->capacity)
	{
		int newcapacity = s1->capacity == 0 ? 4 : s1->capacity * 2;
		SQ* tmp = (SQ*)realloc(s1->arr, newcapacity * sizeof(SQ) * 2);
		if (tmp == NULL)
		{
			printf("realloc fail");
			exit(-1);
		}
		else
		{
			s1->arr = tmp;
			s1->capacity = newcapacity;
		}
	}
}

void SeqListpushBack(SL* s1, SQ x)
{
	SeqListCheckcapacity(s1);
	s1->arr[s1->size] = x;
	s1->size++;
}


void SeqListpushFront(SL* s1, SQ x)
{
	SeqListCheckcapacity(s1);
	int end = s1->size - 1;
	for (end; end >= 0; end--)
	{
		s1->arr[end + 1] = s1->arr[end];
	}
	s1->arr[0] = x;
	s1->size++;
}

void SeqListpopBack(SL* s1)
{
	s1->size--;
}


void SeqListpopFront(SL* s1)
{
	for (int i = 0; i < s1->size-1; i++)
	{
		s1->arr[i] = s1->arr[i + 1];
	}
	s1->size--;
}

void SeqListpushDesignate(SL* s1, int Y, SQ x)
{
	assert(Y <= s1->size);
	SeqListCheckcapacity(s1);
	int end = s1->size - 1;
	for (int i = s1->size - 1; i >= Y-1; i--)
	{
		s1->arr[i + 1] = s1->arr[i];
	}
	s1->arr[Y - 1] = x;
	s1->size++;
}

void SeqListpopDesignate(SL* s1, int Y)
{
	assert(Y <= s1->size);
	int f = Y - 1;
	for (f; f < s1->size; f++)
	{
		s1->arr[f] = s1->arr[f + 1];
	}
	s1->size--;
}

